package com.sz.stock.controller;

import com.sz.stock.client.MarketClient;
import com.sz.stock.client.TradingClient;
import com.sz.stock.common.ResultBean;
import com.sz.stock.model.Entrust;
import com.sz.stock.model.POS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;


@RestController
@RequestMapping("/cli")
public class ClientController {

    @Autowired
    private TradingClient tradingClient;

    private MarketClient marketClient;

    @Autowired
    public void setMarketClient(MarketClient marketClient) {
        this.marketClient = marketClient;
    }

    // 1.失焦事件通过用户和股票编号查询出的 股票名,可用数量;
    @RequestMapping("/trading/selectPos")
    public ResultBean selectPos(POS pos) {     //股票代码
       return tradingClient.selectPos(pos);
    }


    @RequestMapping("/stock/test")
    public ResultBean testStock() {
        return marketClient.testService();
    }

    @RequestMapping("/stock/testparam")
    public ResultBean testparamStock(String name, String pwd) {
        return marketClient.testparamService(name, pwd);
    }

//    添加委托
    @RequestMapping("/trading/addEntrust")
    public ResultBean addEntrust(Entrust entrust) {               // 2.卖出数据url参数: 用户id 股票代码,股票名称,卖出价格,卖出数量
        return tradingClient.addEntrust(entrust);
    }

    @RequestMapping("getMarketData")
    public ResultBean getMarketData(String code,String scale, String datalen) {
        return marketClient.getMarketData(code,scale,datalen);
    }

    @RequestMapping("getAllStock")
    public ResultBean getAllStock() {
        return marketClient.getAllStock();
    }

    @RequestMapping("/stock/pageInit")
    public ResultBean pageInit(@RequestParam(value = "stockCode" ,required = false) String stockCode) {
        if(stockCode==null){
            stockCode="sz000001";
        }
        return marketClient.pageInit(stockCode);
    }

    //  查询委托信息 (未成交 部成)
    @RequestMapping("/trading/keywordEntrust")
    public ResultBean keywordEntrust(){
        return tradingClient.keywordEntrust();
    }

    //  查询委托信息 (未成交 部成) 带分页
    @RequestMapping("/trading/keywordEntrust2")
    public ResultBean keywordEntrust2(int pageIndex, int pageSize,int userid,@RequestBody(required = false) String stockEntrustCode){
        return tradingClient.keywordEntrust2(pageIndex,pageSize,userid,stockEntrustCode);
    }

    //  查询委托信息  带分页
    @RequestMapping("/trading/keywordEntrust3")
    public ResultBean keywordEntrust3(int pageIndex, int pageSize,int userid,@RequestBody(required = false) Entrust entrust){
        return tradingClient.keywordEntrust3(pageIndex,pageSize,userid,entrust);
    }

    //  股票撤单
    @RequestMapping("/trading/cancel")
    public ResultBean cancelStock(@RequestBody ArrayList<Integer> stockEntrustIds){
        return  tradingClient.cancelStock(stockEntrustIds);
    }

    /**
     *  查询 根据委托ID查询一条委托信息
     */
    @RequestMapping("/trading/selectEntrustById")
    public ResultBean selectEntrustById(@RequestBody ArrayList<Integer> stockEntrustIds){
        int stockEntrustId= stockEntrustIds.get(0);
        return tradingClient.selectEntrustById(stockEntrustId);
    }

    /**
     *  修改委托价格(未成交,部成) 只能修改时间
     */
    @RequestMapping("/trading/updateMoney")
    public ResultBean updateMoney(@RequestBody Entrust entrust){
        return tradingClient.updateMoney(entrust);
    }

    /**
     *  股票撤销功能2
     *      撤全部买入 撤全部
     */
    @RequestMapping("/trading/canel2")
    public ResultBean canelStock2(@RequestBody int userid){
        return tradingClient.canelStock2(userid);
    }

    /**
     *  股票撤销功能3
     *      撤全部卖出
     */
    @RequestMapping("/trading/canel3")
    public ResultBean canelStock3(@RequestBody int userid){
        return tradingClient.canelStock3(userid);
    }

    /**
     *  查询 所有成交信息 带分页
     */
    @RequestMapping("/trading/allDeal2")
    public ResultBean allDeal2(int pageIndex, int pageSize,int userid,@RequestBody(required = false) String stockDealCode){
        return tradingClient.allDeal2(pageIndex,pageSize,userid,stockDealCode);
    }

    //  查询个人持仓信息带分页
    @RequestMapping("/pos/pageSearch")
    public ResultBean getMyPos(int userid,int pageIndex, int pageSize,@RequestBody(required = false) String searchCode){
        return tradingClient.getMyPos(userid,pageIndex,pageSize,searchCode);
    }
    //  查询所有股票价格
    @RequestMapping("/stock/getprices")
    public ResultBean getPrices(){
        return marketClient.getPrices();
    }
    //  查询所有股票价格,并存入redis中
    @RequestMapping("/stock/getMarketPrice")
    public ResultBean getMarketPrice(){
        return marketClient.getMarketPrice();
    }
}
